package com.example.demo;

import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo;
import lombok.Data;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;

/**
 * @author admin
 * @version 1.0.0
 * @time 2021/4/12 22:50
 * @desc TODO
 */
@Data
@Component
public class HealthCheckImpl implements HealthCheckHandler, HealthIndicator {

    private boolean isHealth = true;

    @Override
    public Health health() {
        if (isHealth) {
            return new Health.Builder(Status.UP).build();
        } else {
            return new Health.Builder(Status.DOWN).build();
        }
    }

    /**
     * eureka客户端发送心跳获取状态
     * @param currentStatus
     * @return
     */
    @Override
    public InstanceInfo.InstanceStatus getStatus(InstanceInfo.InstanceStatus currentStatus) {

        Status status = this.health().getStatus();
        if (status.equals(Status.UP)) {
            return InstanceInfo.InstanceStatus.UP;
        } else {
            return InstanceInfo.InstanceStatus.DOWN;
        }
    }
}